package com.pzhu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.mapper.*;
import com.pzhu.model.entity.*;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.model.vo.*;
import com.pzhu.service.ThaliService;
import com.pzhu.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : zqh
 * @Date : 2023/12/3 0003 下午 4:58
 * @Version: 1.0
 */
@Service
@Slf4j
public class ThaliServiceImpl extends ServiceImpl<ThaliMapper, Thali> implements ThaliService {

    @Autowired
    private ThaliMapper thaliMapper;
    @Autowired
    private ThaliImageMapper thaliImageMapper;
    @Autowired
    private SpecsMapper specsMapper;
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private ThaliCareerMapper thaliCareerMapper;
    @Autowired
    private CareerMapper careerMapper;

    @Override
    public ResponseResult findHomeData() {

        Map<String, Object> map = new HashMap<>();
        map.put("swiperList",getHomeData("is_swiper",true));
        map.put("majorList",getHomeData("is_major",false));
        map.put("preferredList",getHomeData("is_preferred",false));
        return ResponseResult.ok(map);
    }

    @Override
    public ResponseResult findTree(Long id) {
        Thali thali = thaliMapper.selectById(id);
        ThaliTreeVo thaliTreeVo = BeanCopyUtils.copyBean(thali, ThaliTreeVo.class);
        List<Thali> thaliList = thaliMapper.selectList(new LambdaQueryWrapper<Thali>().eq(Thali::getParentId, id));
        List<ThaliTreeVo> thaliTreeVos = BeanCopyUtils.copyBeanList(thaliList, ThaliTreeVo.class);
        thaliTreeVo.setChildren(thaliTreeVos);
        List<ThaliTreeVo> list = new ArrayList<>();
        list.add(thaliTreeVo);

        Map<String,Object> map = new HashMap<>();
        map.put("thaliList",list);
        return ResponseResult.ok(map);
    }

    @Override
    public ResponseResult finAllByTree() {
        ThaliTreeVo treeVo = thaliMapper.findAllByTree(0);
        Map<String, Object> map = new HashMap<>();
        map.put("thaliList",treeVo);
        return ResponseResult.ok(map);
    }

    @Override
    public ResponseResult detail(Long id) {
        Thali thali = thaliMapper.selectById(id);
        ThaliDetailVo thaliDetailVo = BeanCopyUtils.copyBean(thali, ThaliDetailVo.class);

        thaliDetailVo.setBanner(selectImg(id, "is_swiper"));
        thaliDetailVo.setPhoto(selectImg(id, "is_info"));
        //查询careerId
        ThaliCareer thaliCareer = thaliCareerMapper.selectOne(new LambdaQueryWrapper<ThaliCareer>().eq(ThaliCareer::getThaliId, id));
        thaliDetailVo.setCareerId(thaliCareer.getCareerId());
        Career career = careerMapper.selectOne(new LambdaQueryWrapper<Career>().eq(Career::getId, thaliCareer.getCareerId()));
        thaliDetailVo.setCareerName(career.getName());
        //查询规格信息
        List<Specs> specsList = specsMapper.selectList(new LambdaQueryWrapper<Specs>().eq(Specs::getThaliId, id));
        List<SpecsVo> specsVoList = BeanCopyUtils.copyBeanList(specsList, SpecsVo.class);
        thaliDetailVo.setSpecsList(specsVoList);

        List<BigDecimal> salaries  = specsVoList.stream()
                .map(SpecsVo::getPrice)
                .collect(Collectors.toList());

        // 找到最小值
        BigDecimal minValue = salaries.stream()
                .min(BigDecimal::compareTo)
                .orElse(null);

        // 找到最大值
        BigDecimal maxValue = salaries.stream()
                .max(BigDecimal::compareTo)
                .orElse(null);

        thaliDetailVo.setPrice(minValue + " ~ " + maxValue);

        //查找评论
        List<ReviewVo> reviewVoList = reviewMapper.selectInfo(id);
        reviewVoList.forEach(v->{
            v.setEvaluatePhotos(reviewMapper.selectPhoto(v.getId()));
        });

        thaliDetailVo.setEvaluateList(reviewVoList);
        Map<String,Object> map = new HashMap<>();

        map.put("detail",thaliDetailVo);

        return ResponseResult.ok(map);
    }

    @Override
    public List<ThaliTreeVo> buildTreeMenu(List<ThaliTreeVo> thaliTreeVoList) {
        List<ThaliTreeVo> resultList = new ArrayList<>();
        log.info("你的信息为");
        for (ThaliTreeVo thaliTreeVo : thaliTreeVoList) {
            // 寻找子节点
            for (ThaliTreeVo e : thaliTreeVoList) {
                if (e.getParentId().equals(thaliTreeVo.getId())) {
                    thaliTreeVo.getChildren().add(e);
                }
            }
            // 判断父节点，添加到集合
            if(thaliTreeVo.getParentId()==0L){
                resultList.add(thaliTreeVo);
            }
        }
        return resultList;
    }

    @Override
    public ResponseResult listBuildTree() {
        List<ThaliTreeVo> thaliList = thaliMapper.selectInfo();
        return ResponseResult.ok().put("thaliList",buildTreeMenu(thaliList));
    }

    @Override
    public ResponseResult thaliMoney() {
        List<ThaliMoneyVo> list = thaliMapper.thaliMoney();
        return ResponseResult.ok().put("list",list);
    }

    @Override
    public ResponseResult thaliCount() {
        //查询所有的大类
        List<ThaliCountVo> bigList = thaliMapper.thaliBigCount();
        for (ThaliCountVo thaliCountVo : bigList) {
            thaliCountVo.setCount(0);
        }
        List<ThaliCountVo> bigListAll = new ArrayList<>();
        bigList.forEach(v->
                {
                    try {
                        bigListAll.add((ThaliCountVo) v.clone());
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                }
        );

        //成功单量
        List<ThaliCountVo> list = thaliMapper.thaliCount(2);

        //所有单量
        List<ThaliCountVo> allList = thaliMapper.thaliCount(null);


        bigList.forEach(v->{
            list.forEach(t->{
                if (v.getId().equals(t.getId())){
                    v.setCount(v.getCount()+t.getCount());
                }
            });
        });


        bigListAll.forEach(v->{
            allList.forEach(t->{
                if (v.getId().equals(t.getId())){
                    v.setCount(v.getCount()+t.getCount());
                }
            });
        });


        return ResponseResult.ok().put("list",bigList).put("allList",bigListAll);
    }

    private List<String> selectImg(Long id,String field){

        QueryWrapper<ThaliImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(field,true).eq("thali_id",id);
        List<ThaliImage> items = thaliImageMapper.selectList(queryWrapper);
        return items.stream().map(ThaliImage::getUrl).collect(Collectors.toList());// 取出其中一列
    }

    public List<ThaliHomeVo> getHomeData(String query, Boolean sort){
        QueryWrapper<Thali> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(query, true);
        if (sort) {
            queryWrapper.orderByAsc("sort");
        }
        List<Thali> thaliList = thaliMapper.selectList(queryWrapper);
        return BeanCopyUtils.copyBeanList(thaliList, ThaliHomeVo.class);
    }
}
